package net.caixiaomi.info.shopkeeper;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.logger.Timber;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.UserInfoModel;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.ToastUtil;
import net.caixiaomi.info.widgets.DefLoading;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by mac on 2018/11/14.
 */

public class ShopSetPassActivity extends BaseActivity implements TextWatcher {

    @BindView(R.id.divider)
    View mOriginalDivider;
    @BindView(R.id.phone)
    EditText mPhoneTv;
    @BindView(R.id.password_orign)
    EditText mOriginalPW;
    @BindView(R.id.password_new)
    EditText mNewPW;
    @BindView(R.id.btn_reset)
    TextView mBtnDone;

    @OnClick(R.id.btn_reset)
    void toDone() {
        try {
            AppHelper.hideKeyboard(getCurrentFocus());
            String originalPw = mOriginalPW.getText().toString().trim();
            String newPw = mNewPW.getText().toString().trim();
            String mobile = mPhoneTv.getText().toString().trim();

            if (TextUtils.isEmpty(originalPw)) {
                ToastUtil.s("输入6位数字原密码");
                return;
            }
            if (TextUtils.isEmpty(newPw)) {
                ToastUtil.s("输入6位数字新密码");
                return;
            }

            mLoading.show();

            JSONObject params = new JSONObject();
            params.put("loginSource", "1");
            params.put("mobile", mobile);
            params.put("newPwd", newPw);
            params.put("password", originalPw);
            RequestService requestService = RetrofitManage.getInstance().getRequestService();
            Call<BaseCallModel<UserInfoModel>> baseCallModelCall = requestService.resetPassXN(params);
            baseCallModelCall.enqueue(new ResponseCallback<BaseCallModel<UserInfoModel>>(this) {
                @Override
                public void onSuccess(BaseCallModel<UserInfoModel> data) {
                    mLoading.dismiss();
                    ToastUtil.s(data.msg);
                    UserInfoModel userInfo = data.data;
                    userInfo.setHasPass("1");
                    CommonApp.setUserInfo(userInfo);
//                    onBackPressed();
                    setResult(1001);
                    finish();
                }

                @Override
                public void onFail(ResponseError error) {
                    mLoading.dismiss();
                    if (error.getCode() >= 300000 && error.getCode() <= 310000) {
                        ToastUtil.s(error.getMessage());
                    }
                }

                @Override
                public void onError() {
                    mLoading.dismiss();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int layout() {
        return R.layout.shop_set_pass_layout;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoading = new DefLoading(this);
        mOriginalPW.addTextChangedListener(this);
        mPhoneTv.addTextChangedListener(this);
        mNewPW.addTextChangedListener(this);
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        Timber.d("beforeTextChanged == " + s.toString());
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String text = s.toString();
        if (!TextUtils.isEmpty(text)) {
            char c = s.charAt(s.length() - 1);
            if (Character.isSpaceChar(c)) {
                s.delete(s.length() - 1, s.length());
            }
        }
    }

}
